package com.yan;

import com.rabbitmq.client.*;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Author: 汪焰
 * @Date: 2021/7/8 19:55
 */
public class Producer {


    /**
     * 神坑 下面的 交换器、队列名字 不能改 会报错
     *
     * 必须以 dlx 结尾
     *
     * @Author: 汪焰
     * @Date: 2021/7/8 21:37
     */
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri("amqp://root:root@1.116.195.238:5672/%2f");

        try (final Connection connection = factory.newConnection();
             final Channel channel = connection.createChannel()) {

            //声明一个 死信队列
            channel.exchangeDeclare("ex.dlx", "direct", true);
            channel.queueDeclare("queue.dlx", true, false, false, null);
            channel.queueBind("queue.dlx", "ex.dlx", "key.dlx");

            Map<String, Object> arguments = new HashMap<>();
            //指定消息队列中的消息过期时间
            arguments.put("x-message-ttl", 10000);
            //指定死信队列交换器 ： 过期消息 通过死信交换器 发送到死信队列
            arguments.put("x-dead-letter-exchange", "ex.dlx");
            //指定死信交换器的路由键
            arguments.put("x-dead-letter-routing-key", "key.dlx");

            //声明一个 正常队列
            channel.exchangeDeclare("ex.biz", "direct", true);
            channel.queueDeclare("queue.biz", true, false, false, arguments);
            channel.queueBind("queue.biz", "ex.biz", "key.biz");

            //发送消息
            channel.basicPublish("ex.biz", "key.biz", null, "orderid.45789987678".getBytes());

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
